package com.cqjtu.education.controller;

import com.cqjtu.education.entity.Work;
import com.cqjtu.education.service.WorkService;
import com.cqjtu.education.utils.Result;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Work)表控制层
 *
 * @author makejava
 * @since 2022-04-20 15:51:08
 */
@RestController
@Api(tags = "作业相关接口")
@RequestMapping("work")
public class WorkController {
    /**
     * 服务对象
     */
    @Resource
    private WorkService workService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("/queryById")
    @ApiOperation(value = "queryById", notes = "通过id查询单条数据")
    public Result queryById(@RequestParam("id") String id) {
        try {
            return Result.success(workService.queryById(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("查询失败");
    }

    /**
     * 通过任务id查询完成情况
     *
     * @param taskId taskId
     * @return List
     */
    @RequestMapping("/queryByTaskId")
    @ApiOperation(value = "queryByTaskId", notes = "通过任务id查询学生已完成数据")
    public Result queryByTaskId(@RequestParam("taskId") String taskId) {
        try {
            return Result.success(workService.queryByTaskId(taskId));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("查询失败");
    }

    @RequestMapping("/queryByStudentIdAndTaskId")
    @ApiOperation(value = "queryByStudentIdAndTaskId", notes = "查询当前学生是否对该任务提交过作业")
    public Result queryByStudentIdAndTaskId(@RequestParam("studentId") String studentId, @RequestParam("taskId") String taskId) {
        try {
            return Result.success(workService.queryByStudentIdAndTaskId(studentId, taskId));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("查询失败");
    }

    /**
     * 新增数据
     *
     * @param work 实体
     * @return 新增结果
     */
    @RequestMapping("/add")
    @ApiOperation(value = "add", notes = "新增数据，学生首次提交时操作")
    public Result add(@RequestBody Work work) {
        try {
            return Result.success(workService.insert(work));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("查询失败");
    }

    /**
     * 编辑数据
     *
     * @param work 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    @ApiOperation(value = "edit", notes = "修改数据，学生修改答案后二次提交时操作")
    public Result edit(@RequestBody Work work) {
        try {
            return Result.success(workService.update(work));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("查询失败");
    }

    /**
     * 编辑数据
     *
     * @param workId workId
     * @param score  分数
     * @return 编辑结果
     */
    @RequestMapping("/mark")
    @ApiOperation(value = "mark", notes = "老师打分")
    public Result mark(@RequestParam("workId") String workId, @RequestParam("score") String score) {
        try {
            return Result.success(workService.mark(workId, Integer.parseInt(score)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("编辑失败");
    }


    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("deleteById")
    @ApiOperation(value = "deleteById", notes = "删除数据")
    public Result deleteById(@RequestParam("id") String id) {
        try {
            return Result.success(workService.deleteById(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("删除失败");
    }

}

